// 使用原生Cookie API代替js-cookie库
const TokenKey = 'jijuxie_token'
const UserInfoKey = 'jijuxie_user'

// 获取cookie的工具函数
function getCookie(name) {
  const cookies = document.cookie.split(';')
  for (let i = 0; i < cookies.length; i++) {
    const cookie = cookies[i].trim()
    if (cookie.startsWith(name + '=')) {
      return cookie.substring(name.length + 1)
    }
  }
  return null
}

// 设置cookie的工具函数
function setCookie(name, value, days = 7) {
  const date = new Date()
  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000))
  const expires = `expires=${date.toUTCString()}`
  document.cookie = `${name}=${value};${expires};path=/`
}

// 删除cookie的工具函数
function deleteCookie(name) {
  document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`
}

// 获取token
export function getToken() {
  return localStorage.getItem(TokenKey)
}

// 设置token
export function setToken(token) {
  localStorage.setItem(TokenKey, token)
  return token
}

// 移除token
export function removeToken() {
  localStorage.removeItem(TokenKey)
  return true
}

// 设置用户信息
export function setUserInfo(userInfo) {
  return localStorage.setItem(UserInfoKey, JSON.stringify(userInfo))
}

// 获取用户信息
export function getUserInfo() {
  const userInfoStr = localStorage.getItem(UserInfoKey)
  if (userInfoStr) {
    try {
      return JSON.parse(userInfoStr)
    } catch (e) {
      console.error('解析用户信息失败:', e)
      return null
    }
  }
  return null
}

// 移除用户信息
export function removeUserInfo() {
  return localStorage.removeItem(UserInfoKey)
}

// 清空所有认证信息
export function clearAuth() {
  removeToken()
  removeUserInfo()
} 